package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.GiftReceive;
import com.yunkeji.dao.mapper.GiftReceiveMapper;
import com.yunkeji.dao.vo.GiftReceiveVo;
import com.yunkeji.system.domain.bo.GiftReceiveBo;
import com.yunkeji.system.service.IGiftReceiveService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 礼物接收Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class GiftReceiveServiceImpl implements IGiftReceiveService {

    private final GiftReceiveMapper baseMapper;

    /** 查询礼物接收 */
    @Override
    public GiftReceiveVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询礼物接收列表 */
    @Override
    public TableDataInfo<GiftReceiveVo> queryPageList(GiftReceiveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GiftReceive> lqw = buildQueryWrapper(bo);
        Page<GiftReceiveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询礼物接收列表 */
    @Override
    public List<GiftReceiveVo> queryList(GiftReceiveBo bo) {
        LambdaQueryWrapper<GiftReceive> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GiftReceive> buildQueryWrapper(GiftReceiveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GiftReceive> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSendUid() != null, GiftReceive::getSendUid, bo.getSendUid());
        lqw.eq(bo.getRecUid() != null, GiftReceive::getRecUid, bo.getRecUid());
        lqw.eq(bo.getGiftId() != null, GiftReceive::getGiftId, bo.getGiftId());
        lqw.eq(bo.getPrice() != null, GiftReceive::getPrice, bo.getPrice());
        lqw.like(
                StringUtils.isNotBlank(bo.getGiftName()),
                GiftReceive::getGiftName,
                bo.getGiftName());
        lqw.eq(StringUtils.isNotBlank(bo.getGiftImg()), GiftReceive::getGiftImg, bo.getGiftImg());
        return lqw;
    }

    /** 新增礼物接收 */
    @Override
    public Boolean insertByBo(GiftReceiveBo bo) {
        GiftReceive add = BeanUtil.toBean(bo, GiftReceive.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改礼物接收 */
    @Override
    public Boolean updateByBo(GiftReceiveBo bo) {
        GiftReceive update = BeanUtil.toBean(bo, GiftReceive.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(GiftReceive entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除礼物接收 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
